<?php

namespace app\modules\front\controllers;


use app\models\cacheModel\NewsCategoryModel as NewsCategoryCacheModel;
use app\models\tableModel\NewsModel;
use yii\helpers\ArrayHelper;

/**
 * [新闻分类]页面展示控制器
 */
class NewsController extends BaseController
{

    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => \yii\filters\AccessControl::className(),
                'rules' => [
                    [ // 必须登录才能访问
                        'actions' => [],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                    [ // 无需登录即可访问 - 空的请保证里面有一个空字符串
                        'actions' => ['detail'],
                        'allow' => true,
                        'roles' => ['?']
                    ]
                ],
                'denyCallback' => function ($rule, $action) {

                    // 未登录检测
                    if (\Yii::$app->user->isGuest) {
                        exit($this->showError('请先登录', 401, [
                            'title' => '您还未登录'
                        ]));
                    }

                    // 其余情况
                    exit($this->showError('该页面暂时无法访问', 403));
                }
            ]
        ];
    }

    /**
     * 文章详情
     * @return mixed
     */
    public function actionDetail()
    {

        ### 查询文章
        // 参数
        $id = $this->get('id');
        if (!$id) return $this->showError('文章未找到', 404);
        // 详情
        $detail = NewsModel::loadModel($id);
        if (!$detail) return $this->showError('文章不存在', 404);
        // 状态判断
        $statusOpen = NewsModel::getStatusOpen();
        if ($detail->getAttribute('status') != $statusOpen) return $this->showError(
            '文章暂时无法访问', 404
        );
        $newsDetail = NewsModel::formatData($detail->getAttributes());

        ### 分类信息
        // 详情
        $category = NewsCategoryCacheModel::loadModel($detail->getAttribute('cate_id'));
        if (!$category || $category->getAttribute('status') != $statusOpen) {
            return $this->showError('文章暂时无法访问', 404, [
                'reasons' => ['分类未找到或未开启']
            ]);
        }

        ### 分类模板
        $tempPath = $category->getTemplate(true);
        // 防止双斜杠
        $tempPath = str_replace('//', '/', $tempPath);
        // 项目目录，如：/var/www/html
        // 模板文件是否存在检测
        if (!file_exists(\Yii::getAlias($tempPath))) {
            return $this->showError('文章暂时无法访问', 404, [
                'reasons' => ['模板未找到']
            ]);
        }

        ### [SEO]信息
        $replaceData = [
            '{CATE_NAME}' => $category['name'],
            '{NEWS_TITLE}' => $detail['title'],
            '{NEWS_KEYWORDS}' => implode(',', $detail['keywords']),
            '{NEWS_DESC}' => $detail['description']
        ];
        // 文章 是否开启SEO 标题
        if ($detail->isOpenSeo()) $this->setSeoTitle($detail->getSeoTitle(), $replaceData);
        // 分类 是否开启SEO 标题
        elseif ($category->isOpenSeo()) {
            $this->setSeoTitle($category->getSeoTitle(true), $replaceData);
        }

        // 文章 是否开启SEO 关键词
        if ($detail->isOpenSeo()) $this->setSeoKeywords($detail->getSeoKeywords(), $replaceData);
        // 分类 是否开启SEO 关键词
        elseif ($category->isOpenSeo()) {
            $this->setSeoKeywords($category->getSeoKeywords(true), $replaceData);
        }

        // 是否开启SEO 说明
        if ($detail->isOpenSeo()) {
            $this->setSeoDescription($detail->getSeoDescription(), $replaceData);
        } elseif($category->isOpenSeo()){
            $this->setSeoDescription($category->getSeoDescription(true), $replaceData);
        }

        return $this->render($tempPath, ArrayHelper::merge($newsDetail, [
            'cate_name' => $category['name'],
            'cate_id' => $category['id']
        ]));
    }
}
